package org.huangrui.redis.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author hr
 * @Create 2024-05-01 13:43
 */
@Slf4j
@Service
public class OrderService {
    public static final String ORDER_KEY = "ord:";

    @Resource
    private RedisTemplate<String, String> redisTemplate;
    // @Resource
    // private org.springframework.data.redis.core.StringRedisTemplate StringRedisTemplate;

    public void addOrder()
    {
        int keyId = ThreadLocalRandom.current().nextInt(1000)+1;
        String serialNo = UUID.randomUUID().toString();

        String key = ORDER_KEY+keyId;
        String value = "京东订单:"+serialNo;

        redisTemplate.opsForValue().set(key, value);
        // StringRedisTemplate.opsForValue().set(key,value);

        log.info("***key:{}",key);
        log.info("***value:{}",value);
    }

    public String getOrderById(Integer keyId)
    {
        return redisTemplate.opsForValue().get(ORDER_KEY + keyId);
        //return StringRedisTemplate.opsForValue().get(ORDER_KEY + keyId);
    }

}
